package com.junqson.je.content.controller;

import com.junqson.je.common.UserHolder;
import com.junqson.je.common.annotation.LoginRequire;
import com.junqson.je.common.model.ApiResult;
import com.junqson.je.content.model.CommentModel;
import com.junqson.je.content.model.dto.CommentDTO;
import com.junqson.je.content.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/11 9:38
 * @Version 1.0
 */
@RequestMapping("comment")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发评论
     */
    @RequestMapping("add")
    @LoginRequire
    public ApiResult addComment(CommentDTO dto) {
        if (dto == null) {
            return ApiResult.error("dto is null");
        }
        commentService.addComment(dto);
        return ApiResult.success();
    }


    /**
     * 用户删评论
     */
    @LoginRequire
    @RequestMapping("delete")
    public ApiResult deleteComment(@RequestParam Long commentId) {
        CommentModel model = commentService.getById(commentId);
        if (model.getUserId() != UserHolder.get().getUserId()) {
            return ApiResult.error("无权操作");
        }
        commentService.deleteByCommentId(commentId);
        return ApiResult.success();
    }


    /**
     * 管理员删评论
     */
    @RequestMapping("admin/del")
    public ApiResult adminDelComment(@RequestParam Long commentId) {
        commentService.deleteByCommentId(commentId);
        return ApiResult.success();
    }

    /**
     * 分页查询评论
     */
    @RequestMapping("pageByPostId")
    public ApiResult pageByPostId(@RequestParam Long postId,
                                  @RequestParam(required = false) Integer page,
                                  @RequestParam(required = false) Integer size) {
        return ApiResult.success(commentService.pageByPostId(postId, page, size));
    }
}
